package com.example.boxugu.myapplication.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.example.boxugu.myapplication.Fragment.MyFragment;
import com.example.boxugu.myapplication.Moder.LoginModer;
import com.example.boxugu.myapplication.R;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

public class loginActivity extends AppCompatActivity {
    Button mBtn;
    EditText mUsername,mPassword;
    TextView mText,mText2;
    ImageButton mImgbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mBtn=findViewById(R.id.loginbtn);
        mUsername=findViewById(R.id.edit_username);
        mPassword=findViewById(R.id.edit_passwrod);
        mImgbtn=findViewById(R.id.imgbtn_back);
        mImgbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(loginActivity.this,MainActivity.class);
                startActivity(i);
                finish();
            }
        });
        mText=findViewById(R.id.register_onclik);
        mText.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                Intent i=new Intent(loginActivity.this,RegisterActivity.class);
                startActivity(i);
            }
        });
        mText2=findViewById(R.id.forgetpassword_onclik);
        mText2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i=new Intent(loginActivity.this,ForgotPasswordActivity.class);
                startActivity(i);
            }
        });
        mBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RequestParams requestParams = new RequestParams("http://148.70.46.9:8080/boxuegu/bxg/login");
                final String username=mUsername.getText().toString();
                String password=mPassword.getText().toString();

                if (TextUtils.isEmpty(username)){
                    Toast.makeText(loginActivity.this,"请输入用户名",Toast.LENGTH_LONG).show();
                    return;
                }if(TextUtils.isEmpty(password)){
                    Toast.makeText(loginActivity.this,"请输入密码",Toast.LENGTH_LONG).show();
                    return;
                }
                requestParams.addBodyParameter("username",username);
                requestParams.addBodyParameter("password",password);
                x.http().post(requestParams, new Callback.CommonCallback<String>() {
                    @Override
                    public void onSuccess(String result) {
                        Gson gson =new Gson();
                        LoginModer moder=gson.fromJson(result,LoginModer.class);
                        Log.i("loginActivity","sucess"+result);
                        if (moder.code==0){
                            Toast.makeText(loginActivity.this,"登陆成功",Toast.LENGTH_LONG).show();
                            SharedPreferences sp = getSharedPreferences("boxugu", MODE_PRIVATE);
                            SharedPreferences.Editor editor=sp.edit();
                            editor.putString("token",moder.data.token);
                            editor.commit();
                            Intent intent=new Intent(loginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();

                        }else {
                            Toast.makeText(loginActivity.this,"用户名或密码错误",Toast.LENGTH_LONG).show();
                        }

                    }
                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        Log.i("loginActivity","error"+ex.getMessage());

                    }
                    @Override
                    public void onCancelled(Callback.CancelledException cex) {
                    }
                    @Override
                    public void onFinished() {
                    }
                });

            }
        });
    }
}
